export function makeGetBy(expectKey, dependKey) {
  return function getExpectByDependMaker(list) {
    const map = list.reduce((o, t) => ({ ...o, [t[dependKey]]: t }), {});
    return function getExpectByDependExec(value) {
      const obj = map[value];
      return obj ? obj[expectKey] : null;
    };
  };
}

const MAKE_GET_BY_MAP = {
  makeGetLabelByValue: ['label', 'value'],
  makeGetNameByValue: ['name', 'value'],
  makeGetTittleByValue: ['title', 'value'],
  makeGetNameById: ['name', 'id'],
  makeGetLabelById: ['label', 'id']
};

const transformers = Object.keys(MAKE_GET_BY_MAP).reduce(
  (result, method) => ({
    ...result,
    [method]: makeGetBy(...MAKE_GET_BY_MAP[method])
  }),
  {}
);

export const {
  makeGetLabelByValue,
  makeGetNameByValue,
  makeGetTittleByValue,
  makeGetNameById,
  makeGetLabelById
} = transformers;

export default transformers;
